package com.yycx.base.provider.handler;

import com.yycx.common.base.constants.FlymeConstants;
import com.yycx.common.configuration.OpenCommonProperties;
import com.yycx.common.security.TokenUtils;
import com.yycx.common.security.enums.TokenStoreEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.authentication.BearerTokenExtractor;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;


/**
 * 退出登录成功处理器
 *
 * @author zyf
 */
@Component
@Slf4j
public class LogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    @Resource
    private OpenCommonProperties openCommonProperties;

    @Resource
    private DataSource dataSource;

    private BearerTokenExtractor tokenExtractor = new BearerTokenExtractor();

    public LogoutSuccessHandler() {
        // 重定向到原地址
        this.setUseReferer(true);
    }

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        try {
            // 解密请求头
            authentication = tokenExtractor.extract(request);
            if (authentication != null && authentication.getPrincipal() != null) {
                String tokenValue = authentication.getPrincipal().toString();
                log.debug("revokeToken tokenValue:{}", tokenValue);
                String tokenStoreType = FlymeConstants.BASE_TOKEN_STORE;
                TokenStore tokenStore = null;
                if (tokenStoreType.equals(TokenStoreEnum.REDIS.name())) {
                    tokenStore = TokenUtils.buildRedisTokenStore(redisConnectionFactory);
                }
                if (tokenStoreType.equals(TokenStoreEnum.JWT.name())) {
                    tokenStore = TokenUtils.buildJwtTokenStore();
                }
                if (tokenStoreType.equals(TokenStoreEnum.JDBC.name())) {
                    tokenStore = TokenUtils.buildJdbcTokenStore(dataSource);
                }
                // 移除token
                tokenStore.removeAccessToken(tokenStore.readAccessToken(tokenValue));
            }
        } catch (Exception e) {
            log.error("revokeToken error:{}", e);
        }
        super.onLogoutSuccess(request, response, authentication);
    }
}